package com.hd.tiyu.yuejian.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hd.tiyu.yuejian.JavaBean.Item;
import com.hd.tiyu.yuejian.JavaBean.Train;
import com.hd.tiyu.yuejian.JavaBean.User;
import com.hd.tiyu.yuejian.Model.ItemposModel;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/10/16.
 */
//报名
public class EnrolActivity extends AppCompatActivity {
    private Train data;
    private TextView Trainname,leixing,jiage,username,dianhua,shuliang,zongjia,bmnotice;
    private int nub=1,JG=0,ZJ=0;
    private ImageButton add,jian;
    private Button JieSuan;

    private String userid,train_id,paynotice="pppppp",participantname,participanttel="11111",adultnum="1",paytype,pay="330";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enrol);
        initViews();
        initEvents();
        initData();

    }
    private void initViews() {
        add=(ImageButton)findViewById(R.id.enrol_add);
        jian=(ImageButton)findViewById(R.id.enrol_jian);
        Trainname=(TextView)findViewById(R.id.enrol_trainname);
        leixing=(TextView)findViewById(R.id.enrol_leixing);
        jiage=(TextView)findViewById(R.id.enrol_jiage);
        username=(TextView)findViewById(R.id.enrol_name);
        dianhua=(TextView)findViewById(R.id.enrol_dianhua);
        shuliang=(TextView)findViewById(R.id.enrol_shuliang);
        zongjia=(TextView)findViewById(R.id.enrol_zongjia);
        bmnotice=(TextView)findViewById(R.id.enrol_bmnotice);
        JieSuan=(Button)findViewById(R.id.enrol_Jiesuan);

    }
    private void initData() {
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
        ListsModel model=new ListsModel();
        Call<Train> call=model.getTrain(id);
        call.enqueue(new Callback<Train>() {
            @Override
            public void onResponse(Call<Train> call, Response<Train> response) {
                data=response.body();
                userid=data.getUserid();
                train_id=data.getId();
                Trainname.setText(data.getName());
                paytype=data.getDegree();
                int deg=Integer.parseInt(paytype);
                if(deg==1){
                    leixing.setText("入门");
                }else if(deg==2){
                    leixing.setText("菜鸟");
                }else if(deg==3){
                    leixing.setText("进阶");
                }
                JG=Integer.parseInt(data.getPrice());
                jiage.setText(data.getPrice());
                ListsModel model=new ListsModel();
                Call<User>usercall=model.Userinfo(data.getUserid());
                usercall.enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        User userdata=response.body();
                        username.setText(userdata.getUser_name());
                        participantname=userdata.getUser_name();
                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {

                    }
                });
                shuliang.setText(nub+"");
                zongjia.setText(JG*nub+"");
                bmnotice.setText(data.getBmnotice());
            }

            @Override
            public void onFailure(Call<Train> call, Throwable t) {

            }
        });

    }

    private void initEvents() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nub++;
                ZJ=JG*nub;
                shuliang.setText(nub+"");
                zongjia.setText(ZJ+"");
            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(nub>0){
                    nub--;
                    ZJ=JG*nub;
                }
                shuliang.setText(nub+"");
                zongjia.setText(ZJ+"");
            }
        });
        JieSuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ItemposModel model=new ItemposModel();
                Call<String> call=model.PeixunItempos(participantname,train_id,participanttel,adultnum,paytype,pay,paynotice,userid);
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        String success=response.body().toString();

                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });
            }
        });
    }



}